package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.xtm.NewtmConsumptionScore;
import com.ucredit.babel.common.model.third.xtm.NewtmConsumptionScoreData;
import com.ucredit.babel.common.model.third.xtm.NewtmConsumptionScoreDetail;
import com.ucredit.babel.common.model.third.xtm.NewtmConsumptionScoreRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;


/*
 * 新天民消费分
 *
 * @author:szw
 * */

public class XtmConsumptionScoreParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 新天民消费分
     *
     * @param context
     * @param model
     * @param successful
     * @return boolean
     * @throws IOException
     * @throws InterruptedException
     * @author typole
     * @date 2020/12/10 5:08 下午
     */

    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        NewtmConsumptionScoreRecord obj = (NewtmConsumptionScoreRecord) model;
        NewtmConsumptionScoreData data = obj.getData();
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);

        if (data != null) {
            // code 响应吗
            TxSb.append(data.getCode() == null ? "" : data.getCode()).append(CRT001);
            // msg 响应描述
            TxSb.append(data.getMsg() == null ? "" : data.getMsg()).append(CRT001);
            NewtmConsumptionScore data2 = data.getData();
            if (data2 != null) {
                // tradeNo 新天民订单号
                TxSb.append(data2.getTradeNo() == null ? "" : data2.getTradeNo()).append(CRT001);
                // idName 姓名
                TxSb.append(data2.getIdName() == null ? "" : obj.getName() == null ? "" : obj.getName()).append(CRT001);
                // phone 手机号
                TxSb.append(data2.getPhone() == null ? "" : data2.getPhone()).append(CRT001);
                // idCard 身份证号
                TxSb.append(data2.getIdCard() == null ? "" : obj.getIdNumber() == null ? "" : obj.getIdNumber()).append(CRT001);
                // respCode 响应吗
                TxSb.append(data2.getRespCode() == null ? "" : data2.getRespCode()).append(CRT001);
                // respDesc 响应描述
                TxSb.append(data2.getRespDesc() == null ? "" : data2.getRespDesc()).append(CRT001);
                NewtmConsumptionScoreDetail detail = data2.getDetail();
                if (detail != null) {
                    // 消费分
                    TxSb.append(detail.getScore() == null ? "" : detail.getScore()).append(CRT001);
                } else {
                    TxSb.append(CRT001);
                }
            } else {
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
                TxSb.append(CRT001);
            }
            key.set(getKey());
            val.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        return  data != null;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_xtm_consumption_score_inc";
    }

    @Override
    public String getType() {
        return RecordType.NEWTM_CONSUMPTION_SCORE_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "新天民消费分";
    }
}
